package us.gaaoc.framework.webapp.backingbeans.viewOnly;

import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.component.UISelectItem;
import javax.faces.component.html.HtmlSelectOneListbox;
import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import javax.faces.validator.ValidatorException;

import us.gaaoc.framework.businessmodel.Filing;
import us.gaaoc.framework.model.CaseRecord;
import us.gaaoc.framework.webapp.backingbeans.BaseBackingBean;
import us.gaaoc.framework.webapp.backingbeans.caseTypes.StatusType;
import us.gaaoc.framework.webapp.backingbeans.profile.Links;
import us.gaaoc.framework.webapp.util.Messages;

/**
 * 
 * @author tapshals
 * @date May 24, 2006
 * @version: GAJE 1.5
 * 
 */
@SuppressWarnings("unchecked")
public class ChildEnforcementCaseType extends BaseBackingBean{

	/**
	 * Properties
	 */
	
	/** Generated Serial Version UID */
	private static final long serialVersionUID = 2166617078920717706L;

	private HtmlSelectOneListbox statusUI;

	private StatusType status;

	private Filing filing;

	private DataModel childEnforcementFilingTableList;

	private String docketID;

	private UIInput docketIDInput;

	private FacesMessage message;

	private boolean showMessage;

	private String noDetailsMessage;

	private String courtUuid;
	
	/**
	 * Constructor
	 *
	 */
	public ChildEnforcementCaseType() {
		courtUuid = (String) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("courtUuid");
		this.statusUI = new HtmlSelectOneListbox();
		this.status = new StatusType();
		for (Links links : this.status.getMenu()) {
			SelectItem item = new SelectItem(links.getNavigationRule(), links
					.getText());
			UISelectItem uiItem = new UISelectItem();
			uiItem.setValue(item);
			this.statusUI.getChildren().add(uiItem);
		}

		// type = childEnforcementCase (1)
		List caseRecordList = this.getModelFacade().findCaseFilingsByType(1, courtUuid);
		childEnforcementFilingTableList = new ListDataModel();
		childEnforcementFilingTableList.setWrappedData(caseRecordList);

		Map sessionMap = this.getFacesContext().getExternalContext()
				.getSessionMap();
		String message = (String) sessionMap.get("showMessage");
		if (message != null) {
			sessionMap.remove("showMessage");
			this.setShowMessage(true);
			this.setNoDetailsMessage(message);
		}
	}

	/**
	 * @return Returns statusUI
	 */
	public HtmlSelectOneListbox getStatusUI() {
		return statusUI;
	}

	/**
	 * @param statusUI The statusUI to set
	 */
	public void setStatusUI(HtmlSelectOneListbox statusUI) {
		this.statusUI = statusUI;
	}

	/**
	 * @return Returns the filing
	 */
	public Filing getFiling() {
		return filing;
	}

	/** 
	 * @param filing The filing to set
	 */
	public void setFiling(Filing filing) {
		this.filing = filing;
	}

	/**
	 * @return Returns the childEnforcementFilingDataTableList
	 */
	public DataModel getChildEnforcementFilingTableList() {
		return childEnforcementFilingTableList;
	}

	/**
	 * @return Returns the docketID
	 */
	public String getDocketID() {
		return docketID;
	}

	/**
	 * @param docketID The docketID to set
	 */
	public void setDocketID(String docketID) {
		this.docketID = docketID;
	}

	/**
	 * @return Returns the docketIDInput
	 */
	public UIInput getDocketIDInput() {
		return docketIDInput;
	}

	/**
	 * @param docketIDInput The docketIDInput to set
	 */
	public void setDocketIDInput(UIInput docketIDInput) {
		this.docketIDInput = docketIDInput;
	}

	/**
	 * @return Returns the showMessage
	 */
	public boolean isShowMessage() {
		return showMessage;
	}

	/**
	 * @param showMessage The showMessage to set
	 */
	public void setShowMessage(boolean showMessage) {
		this.showMessage = showMessage;
	}

	/**
	 * @return Returns the noDetailsMessage
	 */
	public String getNoDetailsMessage() {
		return noDetailsMessage;
	}

	/**
	 * @param noDetailsMessage The noDetailsMessage to set
	 */
	public void setNoDetailsMessage(String noDetailsMessage) {
		this.noDetailsMessage = noDetailsMessage;
	}

	/**
	 * This method navigates user to the Case Details Page
	 * @return String
	 */
	public String gotoCaseDetailPage() {
		Map sessionMap = this.getFacesContext().getExternalContext()
				.getSessionMap();
		Map requestMap = this.getFacesContext().getExternalContext()
				.getRequestParameterMap();
		String caseUuid = ((String) requestMap.get("caseUuid"));
		sessionMap.put("caseUuid", caseUuid);
		sessionMap.put("from", "ChildEnforcementCaseTypePage");
		return "gotoCSECaseDetailsPage";

	}

	/**
	 * This method navigates user to the corresponding page depending on the 
	 * status of the case record
	 * @return String
	 */
	public String submitStatus() {
		String navigationString = "";
		if (this.getStatusUI().getValue().equals("received"))
			navigationString = "received";
		else if (this.getStatusUI().getValue().equals("imported"))
			navigationString = "imported";
		else if (this.getStatusUI().getValue().equals("rejected"))
			navigationString = "rejected";
		else if (this.getStatusUI().getValue().equals("served"))
			navigationString = "served";
		else if (this.getStatusUI().getValue().equals("non-served"))
			navigationString = "non-served";
		else if (this.getStatusUI().getValue().equals("disposed"))
			navigationString = "disposed";
		else if (this.getStatusUI().getValue().equals("negotiate"))
			navigationString = "negotiate";
		return navigationString;
	}

	/**
	 * This method navigates user to the previous page
	 * @return String
	 */
	public String back() {
		return "back";
	}

	/**
	 * This method search the case record in the database for the user entered docketID
	 * and navigates user to the Case Details Page
	 * @return String
	 */
	public String findCaseDetailsByDocketID() {
		throw new UnsupportedOperationException("findCaseDetailsByDocketID is disabled");
		/*
		Map sessionMap = this.getFacesContext().getExternalContext()
		.getSessionMap();
		//Court court = (Court)sessionMap.get("court");
		CaseRecord c = this.getModelFacade().findCaseRecordByDocketID(
				this.getDocketID(), courtUuid);
		
		if (c != null) {
			sessionMap.put("caseUuid", c.getUuid());
			sessionMap.put("from", "ChildEnforcementCaseTypePage");
			return "gotoCSECaseDetailsPage";
		} else {
			sessionMap.put("showMessage",
					"There is no case details found by the given docketID "
							+ this.getDocketID() + ".");
			return "stay";
		}
		*/
	}

	/**
	 * This method validates the docketID entered by the user
	 * @param context
	 * @param component
	 * @param value
	 * @throws ValidatorException
	 * @throws Exception
	 */
	public void validateDocketID(FacesContext context, UIComponent component,
			Object value) throws ValidatorException, Exception {
		String docketID = (String) value;
		if (!isValidDocketID(docketID.trim())) {
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(message);
		}
	}

	/**
	 * Helper method to validate the docketID entered by the user
	 * @param docketID
	 * @return
	 * @throws Exception
	 */
	protected boolean isValidDocketID(String docketID) throws Exception {
		boolean validField = false;
		char[] charArray = docketID.toCharArray();
		if (!(charArray.length > 10 || charArray.length < 9)) {
			for (char c : charArray) {
				if (!(Character.isDigit(c) || Character.isLetter(c))) {
					message = Messages.getMessage(
							"us.gaaoc.framework.webapp.util.errorMessage",
							"incorrectDocketID", new Object[] { docketID });
					message.setDetail(message.getDetail());
					message.setSeverity(FacesMessage.SEVERITY_ERROR);
					validField = false;
					break;
				} else
					validField = true;
			}
		} else {
			message = Messages.getMessage(
					"us.gaaoc.framework.webapp.util.errorMessage",
					"incorrectDocketID", new Object[] { docketID });
			message.setDetail(message.getDetail());
			message.setSeverity(FacesMessage.SEVERITY_ERROR);
		}
		return validField;
	}

	/**
	 * This method resets the docketID inputField
	 * @return String
	 */
	public String reset() {
		this.setDocketID("");
		return "stay";

	}

	/**
	 * This method navigates user to the History Page
	 * @return String
	 */
	public String gotoHistoryPage() {
		Map sessionMap = this.getFacesContext().getExternalContext()
				.getSessionMap();
		Map requestMap = this.getFacesContext().getExternalContext()
				.getRequestParameterMap();
		String caseUuid = ((String) requestMap.get("caseUuid"));
		sessionMap.put("caseUuid", caseUuid);
		sessionMap.put("from", "ChildEnforcementCaseTypePage");
		return "gotoCaseHistoryPage";
	}
}